﻿using System;
using System.Web.UI;
using System.ComponentModel;

namespace MyLib.Web.UI {
	/// <summary>
	/// Allow multi checked items.
	/// </summary>
	[SupportsEventValidation]
	[ValidationProperty("SelectedValue")]
	[ToolboxData("<{0}:MyCheckBoxList runat=\"server\"></{0}:MyCheckBoxList>")]
	public class MyCheckBoxList : MyListControl, IPostBackDataHandler, INamingContainer {
		/// <summary>
		/// 
		/// </summary>
		private const string MultiValueSeparator = ",";

		private System.Web.UI.HtmlControls.HtmlGenericControl m_controlToRepeat;

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(true), Themeable(false), Category("Behavior"), Description("MyListControl_ShowUnselected")]
		public override bool ShowUnselected {
			get {
				object obj2 = this.ViewState["ShowUnselected"];
				if (obj2 != null) {
					return (bool)obj2;
				}
				return false;
			}
			set {
				this.ViewState["ShowUnselected"] = value;
				if (base.Initialized) {
					base.RequiresDataBinding = true;
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		[DefaultValue(false), Themeable(false), Bindable(true), Category("Behavior"), Description("MyListControl_ReadOnly")]
		public virtual bool ReadOnly {
			get {
				object obj2 = this.ViewState["ReadOnly"];
				if (obj2 != null) {
					return (bool)obj2;
				}
				return false;
			}
			set {
				this.ViewState["ReadOnly"] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="postDataKey"></param>
		/// <param name="postCollection"></param>
		/// <returns></returns>
		public virtual bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection) {
			var hasChanged = false;

			if (base.IsEnabled) {
				this.EnsureDataBound();

				for (int i = 0; i < Items.Count; ++i) {
					var item = Items[i];

					var pkey = string.Concat(postDataKey, "$", item.Id);

					bool flag = postCollection[pkey] != null;

					if (item.Selected != flag) {
						item.Selected = flag;

						hasChanged = true;
					}
				}
			}

			return hasChanged;
		} // end of LoadPostData.

		/// <summary>
		/// 
		/// </summary>
		public virtual void RaisePostDataChangedEvent() {
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="item"></param>
		/// <param name="index"></param>
		protected override void RenderListItem(HtmlTextWriter writer, MyListItem item, int index) {
			if (item.Enabled) {
				ItemStyle.AddAttributesToRender(writer);
				if (!string.IsNullOrEmpty(item.Tooltip)) {
					writer.AddAttribute(HtmlTextWriterAttribute.Title, item.Tooltip);
				}
				writer.RenderBeginTag(HtmlTextWriterTag.Label);

				m_controlToRepeat.ID = item.Id.ToString();
				m_controlToRepeat.Attributes["type"] = "checkbox";
				m_controlToRepeat.Attributes["name"] = m_controlToRepeat.UniqueID;

				if (item.Selected) {
					m_controlToRepeat.Attributes["checked"] = "checked";
				} else {
					m_controlToRepeat.Attributes.Remove("checked");
				}

				if (ReadOnly) {
					m_controlToRepeat.Attributes["disabled"] = "disabled";
				}

				m_controlToRepeat.Attributes["value"] = item.Value;
				m_controlToRepeat.RenderControl(writer);

				writer.WriteEncodedText(item.Text);

				writer.RenderEndTag();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e) {
			base.OnLoad(e);

			if (Page != null) {
				Page.RegisterRequiresPostBack(this);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public MyCheckBoxList() {
			m_controlToRepeat = new System.Web.UI.HtmlControls.HtmlGenericControl("input");
			m_controlToRepeat.EnableViewState = false;
			m_controlToRepeat.ID = "0";
			m_controlToRepeat.ClientIDMode = System.Web.UI.ClientIDMode.Inherit;
			Controls.Add(m_controlToRepeat);

			ShowUnselected = true;
		}
	} // end of MyCheckBoxList.
}
